package com.exartisan.yn_event;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.exartisan.bean.Event;
import com.exartisan.ynevent.BaseActivity;
import com.loopj.android.image.SmartImageView;
import com.exartisan.util.*;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Session.StatusCallback;

public class DetailActivity extends BaseActivity {
	private Button btnGallery;
	private Event event;
	private TextView tvTitle;
	private SmartImageView imgCover;
	private TextView tvPlace;
	private TextView tvTimeFrom;
	private TextView tvTimeTo;
	private TextView tvDes;
	private ImageButton btnShare;

	
	@Override
	protected void setUpActionBar() {
		super.setUpActionBar();
		ActionBar bar = getSupportActionBar();
		bar.setDisplayShowTitleEnabled(false);
		bar.setDisplayShowHomeEnabled(false);
		bar.setDisplayShowCustomEnabled(true);
		bar.setCustomView(R.layout.ab_detailscreen);
	}

	@Override
	protected void initUI() {
		event = getIntent().getParcelableExtra("event"); 
		setContentView(R.layout.activity_detail);
		
		btnGallery = (Button) findViewById(R.id.btnGallery);
		imgCover = (SmartImageView) findViewById(R.id.imgCover);
		tvPlace = (TextView) findViewById(R.id.tvPlace);
		tvTimeFrom = (TextView) findViewById(R.id.tvTimeFrom);
		tvTimeTo = (TextView) findViewById(R.id.tvTimeTo);
		tvDes = (TextView) findViewById(R.id.tvDescription);
		tvTitle  = (TextView) findViewById(R.id.tvTitle);
		btnShare = (ImageButton) findViewById(R.id.btnShare);
		
		imgCover.setImageUrl(event.getCoverUrl());
		tvPlace.setText("Hà Nội");
		tvTimeFrom.setText(DateTimeFormat.format(event.getStartDate(), DateTimeFormat.LONG_VALUE));
		tvTimeTo.setText(DateTimeFormat.format(event.getEndDate(),DateTimeFormat.LONG_VALUE));
		tvDes.setText(event.getArticle());
		
		btnGallery.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getBaseContext(), Gallery.class);
				i.putExtra("eventId", String.valueOf(event.getEventID()));
				startActivity(i);
			}
		});
		
		btnShare.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Session session = Session.getActiveSession();
				if(session==null||session.isClosed()) Session.openActiveSession(getActivity(), true, new StatusCallback() {
					
					@Override
					public void call(Session session, SessionState state, Exception exception) {
						if(session != null){
							if(state.isOpened()){
								FbUtil.ShareToFb(getActivity(), event.getEventName(), event.getDescription(), FbUtil.YN_LINK, event.getCoverUrl());
							}
						}
					}
				}); else
					FbUtil.ShareToFb(getActivity(), event.getEventName(), event.getDescription(), FbUtil.YN_LINK, event.getCoverUrl());
				
			}
		});
	}

	@Override
	protected void initProcess() {
		event = getIntent().getParcelableExtra("event");
		tvTitle.setText(event.getEventName());
	}
	
	private SherlockFragmentActivity getActivity(){
		return this;
	}

}
